// index.js
// 获取应用实例
const app = getApp()

Page({
    data: {
        name: "",
        id: "",
        errMsg: "",
        activeField: "",
        isRemembered: "",
        showPolicy: false,
        count: 3,
        timer: null,
        isZh: app.globalData.isZh,
        baseURL: app.globalData.baseURL,

    },
    onLoad() {
        if (wx.getStorageSync('name')) {
            this.setData({
                name: wx.getStorageSync('name'),
                id: wx.getStorageSync('id'),
                isRemembered: true
            });
        }


        if (wx.getStorageSync('Authorization')) {
            console.log(wx.getStorageSync('Authorization'));
            wx.request({
                url: app.globalData.baseURL + '/Wechat/Api/Login',
                data: {
                    password: wx.getStorageSync('passWord'),
                    username: wx.getStorageSync('userName')
                },
                // data:this.options,
                method: "POST",
                header: {
                    'content-type': 'application/json' // 默认值
                },
                success: ((res) => {
                    console.log(res);
                    if (res.data.code == 200) {
                        wx.setStorageSync('Authorization', res.data.data.Authorization);
                        wx.navigateTo({
                            url: '../binding/binding'
                        });
                    }
                })
            })
        }
    },
    nameInput(e) {
        this.setData({
            name: e.detail.value,
            errMsg: ""
        });
    },
    idInput(e) {
        this.setData({
            id: e.detail.value,
            errMsg: ""
        });
    },
    nameFocus() {
        this.setData({
            activeField: "name"
        });
    },
    idFocus() {
        this.setData({
            activeField: "id"
        });
    },
    inputBlur() {
        this.setData({
            activeField: ""
        });
    },
    check() {
        this.setData({
            isRemembered: !this.data.isRemembered
        });
    },

    formSubmit() {
        console.log(this.data.name);
        // if (!this.data.name.trim() || !this.data.id.trim()) {
        //     return wx.showToast({ title: '请填写完整', icon: 'error', duration: 1000 });
        // }
        wx.showToast({
            title: this.data.isZh ? "登录中" : "loading",
            icon: "loading",
            mask: true,
            duration: 30000
        });
        wx.request({
            url: app.globalData.baseURL + '/Wechat/Api/Login',
            data: {
                password: this.data.id.trim(),
                username: this.data.name.trim()
            },
            // data:this.options,
            method: "POST",
            header: {
                'content-type': 'application/json' // 默认值
            },
            success: ({
                data
            }) => {
                wx.hideToast();
                console.log(data);
                if (data.code != 200) {
                    this.setData({
                        errMsg: this.data.isZh ? "姓名或证件号后六位输入有误" : "Name or 6 digits entered incorrectly",
                        showPolicy: false
                    });
                } else {
                    this.setData({
                        errMsg: "",
                        showPolicy: !wx.getStorageSync('isAgreed'),
                        count: 3
                    });
                    wx.getStorageSync('isAgreed') ? this.goto() : this.countDown();
                    wx.setStorageSync('Authorization', data.data.Authorization);
                    wx.setStorageSync('userName', this.data.name.trim());
                    wx.setStorageSync('passWord', this.data.id.trim());
                    if (this.data.isRemembered) {
                        wx.setStorageSync('name', this.data.name.trim());
                        wx.setStorageSync('id', this.data.id.trim());
                    } else {
                        wx.removeStorageSync('name');
                        wx.removeStorageSync('id');
                    }
                }
            },
            fail: (res) => {
                console.error("登陆失败", res);
                wx.hideToast();
                this.setData({
                    errMsg: this.data.isZh ? "请检查网络连接" : "Please check the network connection",
                    showPolicy: false
                });
            }
        });
    },
    countDown() {
        this.setData({
            timer: setInterval(() => {
                // console.log(this.data.timer)
                if (this.data.count > 0) {
                    this.setData({
                        count: this.data.count - 1
                    });
                } else {
                    clearInterval(this.data.timer);
                    this.setData({
                        count: 0,
                        timer: null
                    });
                }
            }, 1000)
        })
    },
    closePolicy() {
        clearInterval(this.data.timer);
        this.setData({
            showPolicy: false,
            timer: null
        });
    },
    goto() {
        this.closePolicy();
        console.log();
        // if (wx.getStorageSync('deviceId')) {
        //     wx.switchTab({ url: '../operation/operation' });
        // } else
        if (wx.getStorageSync('isRead')) {
            // wx.navigateTo({ url: '../connection/connection' });
            wx.navigateTo({
                url: '../binding/binding'
            });
        } else {
            wx.navigateTo({
                url: '../instructions/instructions?showBtn=1'
            });
        }
        wx.setStorageSync('isAgreed', 1);
    },
    onHide() {
        !this.data.isRemembered && this.setData({
            name: '',
            id: ''
        });
    },
    onShareAppMessage: function () {}
})